import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;

public class MapViewer extends Frame implements ActionListener, MouseListener, MouseMotionListener  {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2923699712237401116L;
	private static final String FILE_OPEN_COMMAND = "FILE_OPEN_COMMAND";
	private static final String EXIT_COMMAND = "EXIT_COMMAND";
	

	private MapCanvas canvas;
	
	
	private int dragX;
	private int dragY;
	
	public MapViewer() {
		super("MapViewer");
		
		File wd = new File("");
		wd = new File(wd.getAbsolutePath());
		
		this.canvas = new MapCanvas(wd);
		
		this.canvas.addMouseListener(this);
		this.canvas.addMouseMotionListener(this);
		
		add(this.canvas, BorderLayout.CENTER);

		MenuBar menuBar = new MenuBar();
		Menu menu = new Menu("File");
		menu.addActionListener(this);
		
//		MenuItem mi = new MenuItem("Open...");
//		mi.setActionCommand(FILE_OPEN_COMMAND);
//		menu.add(mi);
		
		//menu.addSeparator();
		
		MenuItem mi = new MenuItem("Exit");
		mi.setActionCommand(EXIT_COMMAND);
		menu.add(mi);
		
		menuBar.add(menu);
		
		setMenuBar(menuBar);
	}
	
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (FILE_OPEN_COMMAND == command) {
//			FileDialog dialog = new FileDialog(this, "Open file", FileDialog.LOAD);
//			dialog.setVisible(true);
//			
//			String d = dialog.getDirectory();
//			String f = dialog.getFile();
//			if (d != null && f != null) {
//				File file = new File(d, f);
//				//this.filename = file.getAbsolutePath();
//				this.canvas.repaint();
//			}
		} else if (EXIT_COMMAND == command) {
			System.exit(0);
		}
	}
	
	public void mousePressed(MouseEvent e) {
		this.dragX = e.getX();
		this.dragY = e.getY();
	}
	
	public void mouseReleased(MouseEvent e) {
	}
	
	public void mouseClicked(MouseEvent e) {
		canvas.click(e.getX(), e.getY());
	}
	
	public void mouseEntered(MouseEvent e) {
	}
	
	public void mouseExited(MouseEvent e) {
	}
	
	public void mouseMoved(MouseEvent e) {
	}
	
	public void mouseDragged(MouseEvent e) {
		//if (e.getX() != this.lastX || e.getY() != this.lastY) {
			this.canvas.drag(this.dragX - e.getX(),
							 this.dragY - e.getY());
			this.dragX = e.getX();
			this.dragY = e.getY();
//			Point p = this.scrollPane.getScrollPosition();
//			p.translate(this.dragStartX - e.getX(),
//						this.dragStartY - e.getY());
//			this.scrollPane.setScrollPosition(p);
		//}
	}
	
	public static void main(String[] args) {
		MapViewer viewer = new MapViewer();
		viewer.pack();
		viewer.setSize(200, 320);
		viewer.setVisible(true);
	}
}

